<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 论坛
 * @author  Karson
 * @name    bbs.php
 * @since   2013-8-1 22:58:09
 */
class Bbs_Controller extends Controller {
    
    function _init() {
        check_level();
    }
    
    function index($category_id = 0, $page = 1, $pagesize = 10) {
        $where = $category_id > 0 ? "category_id='{$category_id}'" : '1=1';
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}thread WHERE {$where}");
        $total = $result['num'];
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/bbs/index/{$category_id}/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT * FROM {pre}thread WHERE {$where} ORDER BY replytime DESC LIMIT {$offset},{$pagesize}", "list");
        while ($row = $this->db->get_array("list")) {
            $row['category'] = get_category($row['category_id']);
            $row['flag'] = explode(',', $row['flag']);
            $row['lastreply'] = obj2array(json_decode($row['lastreply']));
            $list[] = $row;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list, 'category_id' => $category_id);
        $this->view->assign($data);
        render("bbs/index");
    }

    function add($category_id = 0) {
        if ($this->form->is_submit()) {
            $value = array(
                'user_id' => $this->auth->user_id,
                'category_id' => $this->input->post('category_id'),
                'nickname' => $this->auth->fields['nickname'],
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'createtime' => time(),
                'replytime' => time(),
                'state' => 1
            );
            $id = $this->db->insert("{pre}thread", $value);
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
            echo json_encode($result);
            exit;
        }
        render("bbs/add", array('category_id' => $category_id));
    }

    function edit($id = 0) {
        $show = $this->db->one("SELECT * FROM {pre}thread WHERE id='{$id}'");
        if ($this->form->is_submit()) {
            $value = array(
                'category_id' => $this->input->post('category_id'),
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'modifytime' => time(),
                'state' => 1
            );
            $id = $this->db->update("{pre}thread", $value, array('id' => $id));
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
            echo json_encode($result);
            exit;
        }
        $this->view->assign("show", $show);
        render("bbs/edit");
    }

    function show($id = 0) {
        $show = $this->db->one("SELECT * FROM {pre}thread WHERE id='{$id}'");
        if ($show) {
            $this->db->update("{pre}thread", array("views" => "#+1"), array("id" => $id));
            $show['user'] = $this->auth->get_user_data($show['user_id']);
            $this->view->assign("show", $show);
        }
        ob_start();
        $this->reply($id);
        $reply_list = ob_get_contents();
        ob_clean();
        $this->view->assign("reply_list", $reply_list);
        render("bbs/show");
    }

    function reply($thread_id, $page = 1, $pagesize = 10) {
        $where = "thread_id='{$thread_id}'";
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}reply cp WHERE {$where}");
        $total = $result['num'];
        $pages = ceil($total / $pagesize);
        $page = $page > $pages ? $pages : $page;
        $page = $page < 1 ? 1 : $page;
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/bbs/reply/{$thread_id}/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT ur.*,u.nickname,u.face FROM {pre}reply ur
            LEFT JOIN {pre}user u ON ur.user_id=u.id
            WHERE {$where} ORDER BY ur.createtime ASC LIMIT {$offset},{$pagesize}", "list");
        $i = 1;
        while ($row = $this->db->get_array("list")) {
            $row['floor'] = $offset + $i;
            $list[] = $row;
            $i++;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list, 'thread_id' => $thread_id);
        $this->view->assign($data);
        $this->view->display("bbs/reply");
    }

    function reply_add($thread_id) {
        $thread = $this->db->one("SELECT * FROM {pre}thread WHERE id='{$thread_id}'");
        if ($thread) {
            if ($this->form->is_submit()) {
                $value = array(
                    'thread_id' => $thread_id,
                    'user_id' => $this->auth->user_id,
                    'nickname' => $this->auth->fields['nickname'],
                    'content' => $this->input->post('content'),
                    'createtime' => time(),
                    'state' => 1
                );
                $id = $this->db->insert("{pre}reply", $value);
                $lastreply = addslashes(json_encode(array('nickname' => $this->auth->nickname, 'user_id' => $this->auth->user_id)));
                $this->db->update("{pre}thread", array('replies' => '#+1', 'replytime' => time(), 'lastreply' => $lastreply), array('id' => $thread_id));
                $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
                echo json_encode($result);
                exit;
            }
            $this->view->assign("thread", $thread);
            render("bbs/reply_add");
        }
    }

}